package com.geilizu.dom.controller;

import com.geilizu.dom.dto.SearchDto;
import com.geilizu.dom.entity.Absent;
import com.geilizu.dom.entity.Building;
import com.geilizu.dom.entity.Dormitory;
import com.geilizu.dom.entity.Student;
import com.geilizu.dom.result.ResponseCode;
import com.geilizu.dom.result.ResponseData;
import com.geilizu.dom.service.AbsentService;
import com.geilizu.dom.service.BuildingService;
import com.geilizu.dom.service.DormitoryService;
import com.geilizu.dom.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Absent)表控制层
 *
 * @author makejava
 * @since 2022-07-12 18:27:16
 */
@RestController
@RequestMapping("absent")
public class    AbsentController {
    /**
     * 服务对象
     */
    @Resource
    private AbsentService absentService;
    @Resource
    private BuildingService buildingService;
    @Resource
    private DormitoryService dormitoryService;
    @Resource
    private StudentService studentService;


    @GetMapping("/buildingList")
    public ResponseData buildingList() {
        List<Building> buildingList = buildingService.list();
        return new ResponseData(ResponseCode.SUCCESS, buildingList);
    }

    @GetMapping("/findDormitoryByBuildingId/{id}")
    public ResponseData findDormitoryByBuildingId(@PathVariable("id") Integer id) {
        List<Dormitory> dormitoryList = dormitoryService.list(id);
        return new ResponseData(ResponseCode.SUCCESS, dormitoryList);
    }

    @GetMapping("/findStudentByDormitoryId/{id}")
    public ResponseData findStudentByDormitoryId(@PathVariable("id") Integer id) {
        List<Student> studentList = studentService.queryByDormitoryId(id);
        return new ResponseData(ResponseCode.SUCCESS, studentList);
    }

    @PostMapping("/save")
    public ResponseData save(@RequestBody Absent absent) {
        String createDate = absent.getCreateDate();
        createDate = createDate.substring(0, 10);
        absent.setCreateDate(createDate);
        Integer i = absentService.save(absent);
        if (i > 0) {
            return new ResponseData(ResponseCode.SUCCESS);
        } else {
            return new ResponseData(ResponseCode.FAILED);
        }
    }

    @GetMapping("/list/{page}/{size}")
    public ResponseData list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Map<String, Object> map = absentService.list(page, size);
        return new ResponseData("200", "success", (Long) map.get("count"), map.get("absentList"));
    }

    @PostMapping("/search")
    public ResponseData search(@RequestBody SearchDto searchDto) {
//        System.out.println(searchDto.getKey());
        Map<String, Object> map = absentService.search(searchDto);
        return new ResponseData("200", "success", (Long) map.get("count"), map.get("absentList"));
    }

}

